using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;
using SIS.ENT;

/// <summary>
/// The TravelDocTypeTableDB class is responsible for interacting with the database to retrieve and store information
/// about TravelDocTypeTable objects.
/// </summary>
public class TravelDocTypeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TravelDocTypeTable from the underlying datasource.
/// </summary>
/// <param name="travelDocTypeCode">The unique TravelDocTypeCode of the TravelDocTypeTable in the database.</param>
/// <returns>An TravelDocTypeTable when the TravelDocTypeCode was found in the database, or null otherwise.</returns>
public static TravelDocTypeTable GetItem(short travelDocTypeCode)
{
TravelDocTypeTable myTravelDocTypeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocTypeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@travelDocTypeCode", travelDocTypeCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTravelDocTypeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTravelDocTypeTable;
}
}

/// <summary>
/// Returns a list with TravelDocTypeTable objects.
/// </summary>
/// <returns>A generics List with the TravelDocTypeTable objects.</returns>
public static TravelDocTypeTableList GetList()
{
TravelDocTypeTableList tempList = new TravelDocTypeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocTypeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a TravelDocTypeTable in the database.
/// </summary>
/// <param name="myTravelDocTypeTable">The TravelDocTypeTable instance to save.</param>
/// <returns>The new TravelDocTypeCode if the TravelDocTypeTable is new in the database or the existing TravelDocTypeCode when an item was updated.</returns>
public static Int16 Save(TravelDocTypeTable myTravelDocTypeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocTypeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTravelDocTypeTable.TravelDocTypeCode == -1)
{myCommand.Parameters.AddWithValue("@travelDocTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@travelDocTypeCode", myTravelDocTypeTable.TravelDocTypeCode);
}
if (myTravelDocTypeTable.Description == null){
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myTravelDocTypeTable.Description);
}
if (myTravelDocTypeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myTravelDocTypeTable.Status);
}
if (myTravelDocTypeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTravelDocTypeTable.CreatedDateTime);
}
if (myTravelDocTypeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTravelDocTypeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TravelDocTypeTable from the database.
/// </summary>
/// <param name="travelDocTypeCode">The TravelDocTypeCode of the TravelDocTypeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(short travelDocTypeCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocTypeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@travelDocTypeCode", travelDocTypeCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TravelDocTypeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TravelDocTypeTable FillDataRecord(IDataRecord myDataRecord )
{
TravelDocTypeTable myTravelDocTypeTable = new TravelDocTypeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TravelDocTypeCode")))
{
myTravelDocTypeTable.TravelDocTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TravelDocTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myTravelDocTypeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTravelDocTypeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTravelDocTypeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTravelDocTypeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTravelDocTypeTable;
}
}
